package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.constant.TableEnum;
import com.hbx.logs.annotation.MarkLog;
import com.hbx.logs.OperaTypeEnum;
import com.hbx.dto.role.InsertOrUpdateRoleDto;
import com.hbx.dto.role.SaveMenuDto;
import com.hbx.exception.RspException;
import com.hbx.service.RoleService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2021-11-26
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色处理器")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有角色
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getRoleList",method = RequestMethod.GET)
    @ApiOperation("获取所有角色")
    public Result roleList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        if (pageNum == null || pageNum < 0){
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR,"pageNum传递有误");
        }
        if (pageSize == null || pageSize < 0){
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR,"pageSize传递有误");
        }
        return roleService.getRoleList(pageNum,pageSize);
    }

    /**
     * 新增或修改角色
     * @param dto
     * @param result
     * @return
     */
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    @ApiOperation("添加或修改角色")
    @SensitiveFilter
    @MarkLog(operate = OperaTypeEnum.UPDATE,tables = TableEnum.SYS_ROLE,desc = "管理员添加或修改角色")
    public Result insertOrUpdateRole(@RequestBody @Valid InsertOrUpdateRoleDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        roleService.insertOrUpdateRole(dto,r);
        return r;
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/removeRole",method = RequestMethod.GET)
    @ApiOperation("删除角色")
    @MarkLog(operate = OperaTypeEnum.DELETE,tables = {TableEnum.SYS_ROLE,TableEnum.SYS_ROLE_USER},desc = "管理员删除角色")
    public Result removeRole(@RequestParam("roleId") Integer roleId) {
        Result r = new Result();
        if (roleId == null){
            r.setCode(400).setMsg("roleId不能为空");
            return r;
        }
        roleService.deleteRole(roleId,r);
        return r;
    }

    /**
     * 根据角色查菜单
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/getMenuIdBuRoleId",method = RequestMethod.GET)
    @ApiOperation("根据角色id查菜单")
    public Result getMenuIdBuRoleId(@RequestParam("roleId") Integer roleId){
        Result r = new Result();
        if (roleId == null){
            r.setCode(400).setMsg("角色id不能为空");
            return r;
        }
        roleService.getMenuIdByRoleId(roleId,r);
        return r;
    }

    /**
     * 授权
     * @param dto
     * @param result
     * @return
     */
    @RequestMapping(value = "/savaMenuToRole",method = RequestMethod.POST)
    @ApiOperation("给角色授权")
    public Result savaMenuToRole(@RequestBody @Valid SaveMenuDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        roleService.savaMenuToRole(dto,r);
        return r;
    }

    /**
     * 获取所有可用角色
     * @return
     */
    @RequestMapping(value = "/getRoleAvalList",method = RequestMethod.GET)
    @ApiOperation("获取所有可用角色")
    public Result getRoleAvalList(){
        Result r = new Result();
        roleService.getRoleAvaiList(r);
        return r;
    }
}

